import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Mucd
 * @ClassName Logon
 * @TIME 星期日 2021/1/3
 */
public class Login {
    static ArrayList<String> addUser = new ArrayList<>();
    static ArrayList<String> addPasswd = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    public static void menu() throws IOException, InterruptedException {
        System.out.println("* * * * * * * * * * *");
        System.out.println("Registered And Login");
        System.out.println("1 9 A p p 4");
        System.out.println("* * * * * * * * * * *");
        //Login 主页
        while (true) {
            System.out.println("1.注册 2.登录 0.退出");
            System.out.println("选择功能");
            int integer = sc.nextInt();
            switch (integer) {
                case 1:
                    establish();
                    break;
                case 2:
                    if (loginUser()) {
                        System.out.println("输入密码");
                        //用户名正确 开始输入密码
                        loginPasswd();
                    } else {
                        System.out.println("用户名错误");
                    }
                    break;
                case 0:
                    System.out.println("正在退出 ....");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入功能数字");
            }
        }
    }

    /**
     * 验证用户名是否重复
     * true为重复,false不重复
     * @return flag
     */
    public static boolean repeatUser(String user) {
        boolean flag = false;
        for (String strUser : addUser) {
            //已有用户名和新增用户名对比
            if (strUser.equals(user)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 创建用户名and密码
     */
    public static void establish() throws InterruptedException {
        System.out.println("创建用户名");
        String intUser = sc.next();
        //添加到用户名集合
        if (repeatUser(intUser)) {
            System.out.println("用户名重复,重新添加");
            System.out.println("1.选择其他功能 2.重新添加");
            int reSelect = sc.nextInt();
            try {
                if (reSelect == 1) {
                    Login.menu();
                } else {
                    establish();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return;
        } else {
            addUser.add(intUser);
            System.out.println("用户名 添加完成");
        }
        System.out.println("*================*");
        System.out.println("添加密码");
        String strPasswd = sc.next();
        //添加到密码集合
        addPasswd.add(String.valueOf(strPasswd));
        System.out.println("密码 添加完成");
    }

    /**
     * 验证用户名
     * @return loginUser
     */
    public static boolean loginUser() {
        System.out.println("输入登录用户名");
        String writeUser = sc.next();
        //遍历用户名集合 并验证
        for (String userLog : addUser) {
            if (writeUser.equals(userLog)) {
                System.out.println("用户名正确");
                System.out.println("*================*");
                return true;
            }
        }
        return false;
    }

    /**
     * 验证密码
     */
    public static void loginPasswd() throws IOException, InterruptedException {
        System.out.println("输入登录密码");
        String scPasswd = sc.next();
        for (String strPasswd : addPasswd) {
            //输入的密码和做已有密码对比
            if (scPasswd.equals(strPasswd)) {
                System.out.println("密码正确");
                System.out.println("正在验证用户名和密码 ... ");
                //间隔2.5秒
                Thread.sleep(2500);
                System.out.println("请稍后 ... ");
                Thread.sleep(1000);
                System.out.println("登录成功✔✔✔");
                System.out.println("*================*");
                ManagementSystem managementSystem = new ManagementSystem();
                managementSystem.information();
                return;
            } else {
                System.out.println("密码错误");
            }
        }
    }
}
